﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EWDTAssignment
{
    public partial class ProductInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["ProductID]"] != null || Request.QueryString["ProductID"] != null)
            {

                dvProdInfo.DataSourceID = "dsProduct";

            }
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["accountID"] = 1;
            // int id = Convert.ToInt32(Session["acctID"].ToString());


            Session["ProductID"] = Convert.ToInt32(Request.QueryString["productID"]);
            if (Session["accountID"] != null)
            {
                Response.Redirect("ShoppingCart.aspx");
            }
        }


        protected void dsProduct_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

        }

        protected void dvProdInfo_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {

        }

        protected void imgBtnAddCart_Click(object sender, ImageClickEventArgs e)
        {

            Session["AccountID"] = "1";
            foreach (DetailsViewRow rowitem in dvProdInfo.Rows)
            {
                Label lblpID = (Label)rowitem.FindControl("lblProductID");
                TextBox txtbxQTY = (TextBox)rowitem.FindControl("txtboxQty");
                int accID = int.Parse(Session["AccountID"].ToString());
                int success = App_Code.DBManager.UpdateCartItem(int.Parse (txtbxQTY.Text),accID,int.Parse(lblpID.Text));
                if (success == 1)
                {

                    ClientScript.RegisterStartupScript(
               this.GetType(),
               Guid.NewGuid().ToString(),
               string.Format("alert('{0}')",
                 "Item Added to Cart"),
                 true);
                }
            }



        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            int productID = int.Parse(Request.QueryString["ProductID"].ToString());//url

            int rating = Rating1.CurrentRating;
            int c = App_Code.DBManager.InsertComment(TextBox2.Text, productID, rating, txtbxNickName.Text);
            //   repeat.DataSourceID = "dsComment";
            repeat.DataBind();
            if (c == 1)
            {
                lblSuccess.Visible = true;

            }
            else
            {
                lblSuccess.Visible = false;
            }

            // this.FindControl("CommentArea").ToString()= string.Empty;
        }

        protected void dvProdInfo_ItemCreated(object sender, EventArgs e)
            {
         
            }

        protected void dvProdInfo_DataBound(object sender, EventArgs e)
            {
            if (!IsPostBack)
                {
                if (dvProdInfo.FooterRow.RowType == DataControlRowType.Footer)
                    {
                    Label ss = (Label)dvProdInfo.FindControl("lblspec");

                    ListBox listspec = (ListBox)dvProdInfo.FindControl("listbxSpec");
            
                    int specRowcount = 0;
                    List<string> sList = App_Code.BusinessLogic.FormatSpecForProduct(ss.Text);
                    if (sList.Count != 0)
                        {
                        foreach (string txt in sList)
                            {
                            listspec.Items.Add(txt);

                            specRowcount++;
                            }
                        listspec.Rows = specRowcount + 1;
                        listspec.Enabled = false;   
                        }
                    }
                
                }
            }



    }
}